package com.shimi.erp.service;

import com.shimi.erp.dao.UniversityRepository;
import com.shimi.erp.domain.PageHelper;
import com.shimi.erp.domain.University;
import com.shimi.erp.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

@Service
public class UniversityService {

    @Autowired
    private UniversityRepository universityRepository;

    public Page<University> findAll(University university, PageHelper pageHelper){

        Specification<University> specification = new Specification<University>() {
            @Override
            public Predicate toPredicate(Root<University> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                Predicate p1 = null;
                if(university.getNameEn() != null) {
                    Predicate p2 = criteriaBuilder.like(root.get("nameEn"),university.getNameEn() + "%");
                    if(p1 != null) {
                        p1 = criteriaBuilder.and(p1,p2);
                    } else {
                        p1 = p2;
                    }
                }

                if(university.getNameCn() != null) {
                    Predicate p2 = criteriaBuilder.like(root.get("nameCn"), university.getNameCn() + "%");
                    if(p1 != null) {
                        p1 = criteriaBuilder.or(p1,p2);
                    } else {
                        p1 = p2;
                    }
                }

                if(university.getLocation().getCountry() != null) {
                    Predicate p2 = criteriaBuilder.equal(root.get("location").get("country"), university.getLocation().getCountry());
                    if(p1 != null) {
                        p1 = criteriaBuilder.and(p1,p2);
                    } else {
                        p1 = p2;
                    }
                }

                if(university.getLocation().getCity() != null) {
                    Predicate p2 = criteriaBuilder.equal(root.get("location").get("city"), university.getLocation().getCity());
                    if(p1 != null) {
                        p1 = criteriaBuilder.and(p1,p2);
                    } else {
                        p1 = p2;
                    }
                }

                return p1;
            }
        };

        Pageable pageable = CommonUtil.getPageable(pageHelper);
        Page<University> universities = universityRepository.findAll(specification, pageable);
        return  universities;
    }

    public University addOne(University reqUniversity){

        University university = universityRepository.save(reqUniversity);

        return  university;
    }

    public University updateOne(University reqUniversity){

        University university = universityRepository.save(reqUniversity);

        return  university;
    }

    public University deleteBatch(University reqUniversity){

        University university = universityRepository.save(reqUniversity);

        return  university;
    }
}
